﻿using System;
using System.Web.Http;
using YouWei.Api.Common;
using YouWei.Api.Models;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Service;

namespace YouWei.Api.Controllers
{
    /// <summary>
    /// 微信用户
    /// </summary>
    public class WxUserController : BaseApiController
    {
        private WeChartUserService _service = new WeChartUserService();
        private WeChartTrailService _serviceTrail = new WeChartTrailService();
        private CommentService _commentService = new CommentService();
        private GroupCommentService _serviceGroupComment = new GroupCommentService();
        private UserInfoService _userService = new UserInfoService();

        ///// <summary>
        ///// 添加用户微信信息(前端先请求wxmini拿到unionid和openid)
        ///// </summary>
        ///// <param name="entity"></param>
        ///// <returns></returns>
        //public ResultData<WeChartUser> AddOrUpdateNoAuthCode(WeChartUser entity)
        //{
        //    var result = ResultData<WeChartUser>.Success("添加成功");
        //    entity.Mini_Time = entity.Mini_Time == null ? DefultTime : entity.Mini_Time;
        //    entity.Subscribe_Time = entity.Subscribe_Time == null ? DefultTime : entity.Subscribe_Time;
        //    //判断是否有openId，如果有，则从库里查出来更新
        //    if (!string.IsNullOrWhiteSpace(entity.OpenID))
        //    {
        //        var model = _service.GetByOpenID(entity.OpenID);
        //        if (model != null)
        //        {
        //            entity.CreateDate = model.CreateDate;
        //            entity.UpdateDate = DateTime.Now;
        //            entity.WeChartUserID = model.WeChartUserID;  //lsc 2019-09-18
        //            //entity.MergeTo(model);
        //            _service.Update(entity);
        //            result.data = entity;
        //            return result;
        //        }
        //    }

        //    entity.CreateDate = DateTime.Now;
        //    entity.UpdateDate = DefultTime;
        //    entity.WeChartUserID = Convert.ToInt32(_service.Insert(entity));

        //    result.data = entity;
        //    return result;
        //}

        /// <summary>
        /// 添加用户微信信息(可传授权码，若不传授权码则需要传openid、unionid)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public ResultData<WeChartUser> AddOrUpdate(WeChatUserParam entity)
        {
            var result = ResultData<WeChartUser>.Success("添加成功");

            //处理时间
            entity.Mini_Time = entity.Mini_Time.Year < 1990 ? DefultTime : entity.Mini_Time;
            entity.Subscribe_Time = entity.Subscribe_Time.Year < 1990 ? DefultTime : entity.Subscribe_Time;

            //如果赋值授权码 则获取微信用户的openid
            if (!string.IsNullOrWhiteSpace(entity.AuthCode))
            {
                var wxSessionResult = new WxMiniController().GetSession(entity.AuthCode);
                if (wxSessionResult.status == 0)
                {
                    var wxSession = wxSessionResult.data;
                    entity.OpenID = wxSession.OpenID;
                    entity.UnionID = wxSession.UnionID;
                }
                else
                {
                    return ResultData<WeChartUser>.Error("获取OpenId失败，原因：" + wxSessionResult.message);
                }
            }

            //判断是否有openId，如果有，则从库里查出来更新
            var model = _service.GetByOpenID(entity.OpenID);
            if (model != null)
            {
                entity.WeChartUserID = model.WeChartUserID;
                entity.CreateDate = model.CreateDate;
                entity.UpdateDate = DateTime.Now;
                _service.Update(entity);
                result.data = entity;
            }
            else
            {
                entity.CreateDate = DateTime.Now;
                entity.UpdateDate = DefultTime;
                object id = _service.Insert(entity);
                entity.WeChartUserID = Convert.ToInt32(id);
                result.data = entity;
            }

            //更新用户
            var usr = _userService.GetUserInfoByOpenID(entity.OpenID);
            if (usr != null)
            {
                usr.UserName = entity.NickName;
                usr.UserHead = entity.HeadImgUrl;
                usr.UserDesc = entity.Remark;
                usr.UpdateDate = DateTime.Now;
                usr.Sex = entity.Sex;
                _userService.Update(usr);
            }


            return result;
        }


        /// <summary>
        /// 微信用户行为轨迹添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultData<string> WxUserTrailRecord(WeChartTrail entity)
        {
            var result = ResultData<string>.Success("记录成功");
            entity.TrailDate = DateTime.Now;
            entity.AwayDate = DateTime.Now;
            _serviceTrail.Insert(entity);
            if (entity.TrailType == 1)
            {
                switch (entity.ActionType)
                {
                    //1：转发 2：浏览 3：答题 4：评论 5：点赞，6：取消点赞
                    case 2:
                        _serviceGroupComment.AddPageViews(entity.TrailIdentity);
                        break;
                    case 4:
                        _serviceGroupComment.AddEvaluateCount(entity.TrailIdentity);
                        break;
                    case 5:
                        _serviceGroupComment.AddPraiseCount(entity.TrailIdentity);
                        break;

                }
            }
            if (entity.TrailType == 2)
            {
                switch (entity.ActionType)
                {
                    //1：转发 2：浏览 3：答题 4：评论 5：点赞，6：取消点赞
                    case 2:
                        _commentService.AddPageViews(entity.TrailIdentity);
                        break;
                    case 4:
                        _commentService.AddEvaluateCount(entity.TrailIdentity);
                        break;
                    case 5:
                        _commentService.AddPraiseCount(entity.TrailIdentity);
                        break;
                }
            }

            return result;
        }

    }
}